#include "../mqserver/mq_exchange.hpp"
#include <gtest/gtest.h>

mq::ExchangeManager::Ptr emp;
class Exchange_Test : public testing::Environment
{
public:
    void SetUp() override
    {
        emp = std::make_shared<mq::ExchangeManager>("./test-exchange.db");
    }
    void TearDown() override
    {
        // emp->clear();
    }
private:
    ;
};

TEST(Exchange_Test,insert_test)
{
    std::unordered_map<std::string,std::string> testmap = { {"k1","v1"} , {"k2" , "v2"} };
    emp->declareExchange("exchange1",mq::ExchangeType::DIRECT,true,false,testmap);
    emp->declareExchange("exchange2",mq::ExchangeType::DIRECT,true,false,testmap);
    emp->declareExchange("exchange3",mq::ExchangeType::DIRECT,true,false,testmap);
    emp->declareExchange("exchange4",mq::ExchangeType::DIRECT,true,false,testmap);
    ASSERT_NE(emp.get(),nullptr);
    ASSERT_EQ(emp->size(),4);
}

TEST(Exchange_Test,select_test)
{
    mq::Exchange::Ptr exp = emp->selectExchange("exchange3");
    ASSERT_EQ(exp->name,std::string("exchange3"));
    ASSERT_EQ(exp->type,mq::ExchangeType::DIRECT);
    ASSERT_EQ(exp->durable,true);
    ASSERT_EQ(exp->auto_delete,false);
    ASSERT_EQ(exp->getArgs(),std::string("k1=v1&k2=v2&"));
}

TEST(Exchange_Test,remove_test)
{
    emp->deleteExchange("exchange4");
    mq::Exchange::Ptr exp = emp->selectExchange("exchange4");
    ASSERT_EQ(emp->size(),3);
    ASSERT_EQ(exp.get(),nullptr);
    ASSERT_EQ(emp->exist("exchange1"),true);
    ASSERT_EQ(emp->exist("exchange2"),true);
    ASSERT_EQ(emp->exist("exchange3"),true);
    ASSERT_EQ(emp->exist("exchange4"),false);
}

// TEST(Exchange_Test,clear_test)
// {
//     emp->deleteExchange("exchange4");
//     mq::Exchange::Ptr exp = emp->selectExchange("exchange4");
//     emp->clear();
//     //ASSERT_EQ(emp->size(),3);
//     ASSERT_EQ(exp.get(),nullptr);
//     ASSERT_EQ(emp->exist("exchange1"),false);
//     ASSERT_EQ(emp->exist("exchange2"),false);
//     ASSERT_EQ(emp->exist("exchange3"),false);
//     ASSERT_EQ(emp->exist("exchange4"),false);
//     ASSERT_EQ(emp->size(),0);
// }

int main(int argc,char* argv[])
{
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new Exchange_Test);
    RUN_ALL_TESTS();

    return 0;
}

// std::unordered_map<std::string,std::string> mymap;
// class MyMapTest : public testing::Environment
// {
// public:
//     void SetUp()
//     {
//         std::cout << "单元测试前执行的初始化\n";
//         mymap.insert(std::make_pair("Hello","你好"));
//         mymap.insert(std::make_pair("Bye","再见"));
//     }
//     void TearDown()
//     {
//         std::cout << "单元测试执行完毕的清理\n";
//         mymap.clear();
//     } 
// private:
// };

// TEST(MyMapTest,test1)
// {
//     //ASSERT_EQ(mymap.size(),1);
//     EXPECT_EQ(mymap.size(),1);
// }

// TEST(MyMapTest,test2)
// {
//     EXPECT_EQ(mymap.size(),2);
// }

// int main()
// {
//     testing::InitGoogleTest();
//     RUN_ALL_TESTS();

//     return 0;
// }